<template>
  <div>
    <div class="container">
      <div class="search-main">
        <el-form ref="queryRef" :inline="true" :model="queryParams" label-width="70">
          <el-form-item label="参数名称" prop="parTypeDesc">
            <el-input v-model="queryParams.parTypeDesc" clearable placeholder="请输入参数名称" style="width: 200px"
                      @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" size="" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" size="" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-row class="">
        <el-col :span="24">
          <el-button icon="Plus" plain size="" type="primary" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>

      <vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents">
        <template #action="{ row }">
          <template v-if="hasEditStatus(row)">
            <el-button size="" type="primary" @click="saveRowEvent(row)">保存</el-button>
            <el-button size="" @click="cancelRowEvent()">取消</el-button>
          </template>
          <template v-else>
            <el-button plain size="" type="primary" @click="editRowEvent(row)">编辑</el-button>
            <el-popconfirm title="确实要删除当前项吗?" @confirm="deleteRow(row)">
              <template #reference>
                <el-button color="#ff4444f7" plain size="" type="primary" @click="">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </template>
      </vxe-grid>

      <!-- 添加或修改岗位对话框 -->
      <el-dialog v-model="state.dialogVisible" :close-on-click-modal="false" append-to-body title="新增用户"
                 width="500">
        <el-form ref="postRef" :model="userForm" :rules="rules" label-width="100">
          <el-form-item label="参数代码：" prop="parTypeCode">
            <el-input v-model="userForm.parTypeCode" placeholder="请输入参数代码"/>
          </el-form-item>
          <el-form-item label="参数名称：" prop="parTypeDesc">
            <el-input v-model="userForm.parTypeDesc" placeholder="请输入参数名称"/>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>

    <el-divider border-style="dashed" />

    <DictSub :pid="state.pid"></DictSub>
  </div>


</template>

<script name="Dict" setup>
import {onMounted, reactive, ref, toRefs , defineAsyncComponent} from "vue";
import * as  SystemApi from "@/api/systemApi";
import {ElMessage} from "element-plus";

const DictSub = defineAsyncComponent(() => import("./components/dictSub.vue"));

const gridRef = ref();
const queryRef = ref(null);
const {log} = console;

//组件状态
const state = reactive({
  dialogVisible: false,
  userForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parTypeDesc: '',
  },
  querySubParam: {
    parName:'',
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    parTypeCode: [{required: true, message: "参数代码不能为空", trigger: "blur"}],
    parTypeDesc: [{required: true, message: "参数名称不能为空", trigger: "blur"}],
  },
  pid: null,
});

const {queryParams, userForm , rules} = toRefs(state);

//add用户
async function submitForm() {
  let res = await SystemApi.dictSave(userForm.value);
  if (res.code == 200 && res.data) {
    ElMessage.success("操作成功");
    handleQuery();
  }
  state.dialogVisible = false;
}

function cancel() {
  state.dialogVisible = false;
}

//表格配置
const gridOptions = reactive({
  minHeight: 0,
  emptyText: "暂无数据！",
  keepSource: true,
  headerRowClassName: "table-head-cla",
  border: true,
  showOverflow: true,
  loading: false,
  align: "center",
  rowConfig: {
    isCurrent: true,
    isHover: true,
  },
  size: "small",
  autoResize: true,
  columnConfig: {
    isHover: true,
    resizable: false,
  },
  editConfig: {
    showIcon: false, //是否显示列头编辑图标
    trigger: "manual",
    showStatus: true,
    autoClear: true,
    mode: "row",
  },
  pagerConfig: {
    border: true,
    enabled: true,
    currentPage: 1,
    pageSize: 10,
    total: 0,
    layouts: [
      "Home",
      "PrevJump",
      "PrevPage",
      "Number",
      "NextPage",
      "NextJump",
      "End",
      "Sizes",
      "FullJump",
      "Total",
    ],
  },
  columns: [
    {type: "seq", width: 70},
    {
      field: "parTypeCode",
      title: "参数代码",
      sortable: true,
      editRender: {name: "VxeInput"},
    },
    {
      field: "parTypeDesc",
      title: "参数名称",
      sortable: true,
      editRender: {name: "VxeInput"},
    },
    {field: "createByUserName", title: "创建人"},
    {field: "createTime", title: "创建日期", sortable: true},
    {field: "updateByUserName", title: "修改人"},
    {field: "lastModifyTime", title: "修改日期", sortable: true},
    {title: "操作", slots: {default: "action"}},
  ],
  data: [],
});

//分页事件
const gridEvents = {
  pageChange({pageSize, currentPage}) {
    gridOptions.pagerConfig.currentPage = currentPage;
    gridOptions.pagerConfig.pageSize = pageSize;
    queryParams.value.pageNum = currentPage;
    queryParams.value.pageSize = pageSize;
    handleQuery(queryParams);
  },
  cellClick  ({ row, column }) {
    state.pid = row.id;
  }
};


function handleAdd() {
  state.dialogVisible = true;
}

//查询list
async function handleQuery() {
  gridOptions.loading = true;
  let res = await SystemApi.dictSearch(queryParams.value);
  gridOptions.data = res.data.records;
  gridOptions.pagerConfig.total = res.data.total
  gridOptions.loading = false;
}

function resetQuery() {
  queryRef.value.resetFields();
}

const hasEditStatus = (row) => {
  const $grid = gridRef.value;
  if ($grid) {
    return $grid.isEditByRow(row);
  }
};

const editRowEvent = (row) => {
  const $grid = gridRef.value;
  if ($grid) {
    $grid.setEditRow(row);
  }
};

//编辑用户
const saveRowEvent = async (row) => {
  const $grid = gridRef.value
  if ($grid) {
    $grid.clearEdit().then(async () => {
      let res = await SystemApi.dictSave(row);
      if (res.code == 200 && res.data) {
        ElMessage.success("操作成功");
        handleQuery();
      }

    })
  }

};

//编辑取消
const cancelRowEvent = (row) => {
  const $grid = gridRef.value;
  if ($grid) {
    $grid.clearEdit().then(() => {
      // 还原行数据
      $grid.revertData(row);
    });
  }
};

//删除数据
async function deleteRow(row) {
  let res = await SystemApi.dictDel(row.id);
  if (res.code == 200 && res.data) {
    ElMessage.success("操作成功");
    handleQuery();
  }
}

onMounted(() => {
  handleQuery();
});

</script>

<style lang="scss" scoped>
.container {
  .search-main {
    line-height: 78px;

    .el-form-item {
      margin-bottom: 0px;
    }
  }

  .el-row {
    margin-bottom: 10px;
  }
}
</style>
